package com.habdqn.easybuy.web.front.product;

import com.habdqn.easybuy.entity.Product;
import com.habdqn.easybuy.entity.ProductCategory;
import com.habdqn.easybuy.service.ProductCategoryService;
import com.habdqn.easybuy.service.ProductService;
import com.habdqn.easybuy.service.impl.ProductCategoryServiceImpl;
import com.habdqn.easybuy.service.impl.ProductServiceImpl;
import com.habdqn.easybuy.util.Page;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xing.Jiejian
 * @date 2020/12/1
 */
@WebServlet(name = "FrontListServlet",urlPatterns = "/product/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取分页对象
        Page<Product> page = new Page<>(1,20);
        String strPageNo = request.getParameter("pageNo");
        if (StringUtils.isNotBlank(strPageNo)) {
            page.setPageNo(Integer.parseInt(strPageNo));
        }
        String strPageSize = request.getParameter("pageSize");
        if (StringUtils.isNotBlank(strPageSize)) {
            page.setPageSize(Integer.parseInt(strPageSize));
        }

        // 获取当前需要查询的分类
        ProductCategoryService categoryService = new ProductCategoryServiceImpl();
        ProductService productService = new ProductServiceImpl();

        ProductCategory category = null;

        // 1.父分类编号 topCid
        String strTopCid = request.getParameter("topCid");
        if (StringUtils.isNotBlank(strTopCid)){
            int pid = Integer.parseInt(strTopCid);
            // 根据父分类ID找到所有子分类
            List<ProductCategory> categoryList = categoryService.queryByParentId(pid);
            // 提取所有子分类编号(提取分类集合中的id，使用“,”分隔组装成字符串，例如： “1,2,3”)
            String ids = categoryList.stream().map(p -> {
                return p.getId().toString();
            }).collect(Collectors.joining(","));
            // 根据子分类编号查询商品
            productService.queryByPageAndCategoryIds(ids,page);
            category = categoryService.queryById(pid);
        }
        // 2.子分类编号 subCid
        String strSubCid = request.getParameter("subCid");
        if (StringUtils.isNotBlank(strSubCid)){
            int categroyId = Integer.parseInt(strSubCid);
            Product product = new Product();
            product.setCategoryId(categroyId);
            // 根据分类编号查询商品
            productService.queryByPage(product,page,false);
            category = categoryService.queryById(categroyId);
            strTopCid = category.getParentId().toString();
        }

        // TODO 3.分类or商品名称 cName


        // 4. 没有参数则查询所有商品即全部分类
        if (page.getRows()==null){
            productService.queryByPage(null,page,false);
        }

        request.setAttribute("page",page);
        request.setAttribute("topCid",strTopCid);
        request.setAttribute("subCid",strSubCid);
        request.setAttribute("category",category);
        request.getRequestDispatcher("/WEB-INF/front/product-list.jsp").forward(request,response);
    }
}
